/*支付状态formartter*/
function formatterPaidStatus(value,row,index) {
    if (value ==0) {
        return '<span style="color: grey">尚未付款</span>';
    }else if (value == 1) {
        return '<span style="color: green">已结清</span>';
    }else {
        return '<span style="color: red">尚未结清</span>';
    }
}
/*班级formatter*/
function formatterClz(value,row,index) {
    return value ? value.name : "";
}
/*支付方式formatter*/
function formatterPaymentMethod(value,row,index) {
    return value ? value.name : "";
}

/*支付方式formatter*/
function formatterSaleman(value,row,index) {
    return value ? value.realName : "";
}

function formatterStudentStatus(value,row,index) {
    if (value == 0) {
        return '<span style="color: green">正常</span>';
    } else if (value == 1) {
        return '<span style="color: red">休学中</span>';
    } else if(value == 2) {
        return '<span style="color: grey">退学审核中</span>';
    }else{
        return '<span style="color: black">已经退学</span>';
    }}

$(function () {
    //正式员工数据表
    var formalStudent_datagrid = $("#formalStudent_datagrid");
    //正式员工数据表的工具栏
    var formalStudent_datagrid_toolbar = $("#formalStudent_datagrid_toolbar");
    //正式员工数据的编辑表单
    var formalStudent_edit_form = $("#formalStudent_edit_form");
    //员工编辑弹窗
    var formalstudent_dialog = $("#formalstudent_dialog");
    //转班操作弹窗
    var formalStudent_transfer_dialog = $("#formalStudent_transfer_dialog");
    //转班操作表单
    var formalStudent_transfer_form = $("#formalStudent_transfer_form");
    //休学按钮
    var btn_quit = $("#btn_quit")
    //退学弹窗
    var formalStudent_lostStudent_dialog = $("#formalStudent_lostStudent_dialog");
    //退学表单
    var formalStudent_lostStudent_form = $("#formalStudent_lostStudent_form");
    //升班弹窗
    var formalStudent_upgrade_dialog = $("#formalStudent_upgrade_dialog");
    //升班表单
    var formalStudent_upgrade_form = $("#formalStudent_upgrade_form");
    formalStudent_datagrid.datagrid({
        url:'/formalStudent/list',
        fitColumns:true,
        fit:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        columns:[[
            {field:'studentName',title:'学员姓名',width:100},
            {field:'saleman',title:'营销人员',width:100,formatter:formatterSaleman},
            {field:'totalTuition',title:'总学费',width:100},
            {field:'remainPaid',title:'待缴学费',width:100},
            {field:'alreadyPaid',title:'已缴学费',width:100},
            {field:'paidStatus',title:'缴费状态',width:100,formatter:formatterPaidStatus},
            {field:'enrollTime',title:'入学时间',width:100},
            {field:'school',title:'毕业学校',width:100},
            {field:'tel',title:'联系电话',width:100},
            {field:'clz',title:'所在班级',width:100,formatter:formatterClz},
            {field:'paymentMethod',title:'付款方式',width:100,formatter:formatterPaymentMethod},
            {field:'status',title:'学员状态',width:100,formatter:formatterStudentStatus}
        ]],
        toolbar:formalStudent_datagrid_toolbar,
        onSelect:function (index, data) {
            if (data.status != 0 & data.status != 1) {
                btn_lost
            }
            if (data.status == 0) {
                btn_quit.linkbutton({
                    text:'休学'
                })
            }else if (data.status == 1) {
                btn_quit.linkbutton({
                    text:'复学'
                })
            }
        }
    });

    var cmdObj ={
        //编辑学员信息
        editStudent:function () {
            formalStudent_edit_form.form("clear");
            //获取选中行
            var selectedRow = formalStudent_datagrid.datagrid("getSelected");
            console.log(selectedRow);
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            //处理编辑信息的回显
            /*学员来源回显*/
            selectedRow['client.source.id']=selectedRow.client.source.id;
            /*性别回显*/
            selectedRow['client.gender']=selectedRow.client.gender;
            /*学历回显*/
            selectedRow['client.eduBackground.id']=selectedRow.client.eduBackground.id;
            /*所在班级回显*/
            selectedRow['clz.id']=selectedRow.clz.id;
            /*营销人员回显*/
            selectedRow['saleman.realName']=selectedRow.saleman.realName;
            /*所在校区回显*/
            selectedRow['client.campus.id']=selectedRow.client.campus.id;
            /*学校回显*/
            selectedRow['client.school']=selectedRow.client.school;
            /*专业回显*/
            selectedRow['client.major']=selectedRow.client.major;
            /*工作年限回显*/
            selectedRow['client.yearsOfWorking']=selectedRow.client.yearsOfWorking;
            /*备注回显*/
            selectedRow['client.remark']=selectedRow.client.remark;
            /*qq回显*/
            selectedRow['client.qq']=selectedRow.client.qq;
            /*年龄回显*/
            selectedRow['client.age']=selectedRow.client.age;
            /*地址回显*/
            selectedRow['client.address']=selectedRow.client.address;
            /*邮箱回显*/
            selectedRow['client.email']=selectedRow.client.email;
            /*付款方式回显*/
            selectedRow['paymentMethod.id']=selectedRow.paymentMethod.id;
            formalStudent_edit_form.form("load",selectedRow);
            $(":input").prop('disabled',false);
            formalstudent_dialog.dialog("open");
        },
        //刷新
        reloadStudent:function () {
            formalStudent_datagrid.datagrid("reload");
        },
        /*编辑学生信息的弹出框的保存按钮*/
        saveFormalStudent:function () {
            formalStudent_edit_form.form("submit",{
                url:'/formalStudent/update',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            cmdObj.cancel();
                            cmdObj.reloadStudent();
                        });
                    } else {
                        $.messager.alert("提示", data.msg, "error");
                    }
                }
            })
        },
        /*弹出框的取消按钮*/
        cancel:function () {
            formalstudent_dialog.dialog("close");
        },
        /*查看按钮*/
        viewStudent:function () {
            formalStudent_edit_form.form("clear");
            //获取选中行
            var selectedRow = formalStudent_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            //处理编辑信息的回显
            /*学员来源回显*/
            selectedRow['client.source.id']=selectedRow.client.source.id;
            /*性别回显*/
            selectedRow['client.gender']=selectedRow.client.gender;
            /*学历回显*/
            selectedRow['client.eduBackground.id']=selectedRow.client.eduBackground.id;
            /*所在班级回显*/
            selectedRow['clz.id']=selectedRow.clz.id;
            /*营销人员回显*/
            selectedRow['saleman.realName']=selectedRow.saleman.realName;
            /*所在校区回显*/
            selectedRow['client.campus.id']=selectedRow.client.campus.id;
            /*学校回显*/
            selectedRow['client.school']=selectedRow.client.school;
            /*专业回显*/
            selectedRow['client.major']=selectedRow.client.major;
            /*工作年限回显*/
            selectedRow['client.yearsOfWorking']=selectedRow.client.yearsOfWorking;
            /*备注回显*/
            selectedRow['client.remark']=selectedRow.client.remark;
            /*qq回显*/
            selectedRow['client.qq']=selectedRow.client.qq;
            /*年龄回显*/
            selectedRow['client.age']=selectedRow.client.age;
            /*地址回显*/
            selectedRow['client.address']=selectedRow.client.address;
            /*邮箱回显*/
            selectedRow['client.email']=selectedRow.client.email;
            /*付款方式回显*/
            selectedRow['paymentMethod.id']=selectedRow.paymentMethod.id;
            formalStudent_edit_form.form("load",selectedRow);
            $(":input").prop('disabled','disabled');
            formalstudent_dialog.dialog({
                buttons:''
            })
            formalstudent_dialog.dialog("open");
        },
        /*转班操作*/
        transferClass:function () {
            formalStudent_transfer_form.form("clear");
            //获取选中行
            var selectedRow = formalStudent_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            /*打开转班dialog*/
            formalStudent_transfer_dialog.dialog("open");
        },
        //转班操作弹出框的关闭
        cancelTransfer:function () {
            formalStudent_transfer_dialog.dialog("close")
        },
        /*转班操作的保存*/
        saveTransfer:function () {
            formalStudent_transfer_form.form("submit",{
                url:'/formalStudent/transfer',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            cmdObj.cancelTransfer();
                            cmdObj.reloadStudent();
                        });
                    } else {
                        $.messager.alert("提示", data.msg, "error");
                    }
                }
            })
        },
        /*休学按钮*/
        quitStudent:function () {
            //获取选中行
            var selectedRow = formalStudent_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            var flag = selectedRow.status == 0 ? "<span style='color: red'>休学</span>" : "<span style='color: green;'>正常</span>";
            var statusVal = flag == "<span style='color: red'>休学</span>" ? 1 : 0;
            $.messager.confirm("提示","确定将学员设为" + flag +"状态吗?",function (yes) {
                if (yes) {
                    $.post("/formalStudent/quitStudent",{id:selectedRow.id,statusVal:statusVal},function (data) {
                        if (data.success) {
                            $.messager.alert("提示","操作成功!","info",function () {
                                cmdObj.reloadStudent();
                            })
                        }else {
                            $.messager.alert("提示",data.msg,"error");
                        }
                    })
                }
            })
        },
        /*退学按钮*/
        lostStudent:function () {
            $('#backMoneyAmount_tr').hide();
            //获取选中行
            var selectedRow = formalStudent_datagrid.datagrid("getSelected");
            console.log(selectedRow);
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            formalStudent_lostStudent_form.form("clear");
            //主要是为了回显出id
            formalStudent_lostStudent_form.form("load", selectedRow);
            formalStudent_lostStudent_dialog.dialog("setTitle", "退学");
            $('#drawBackAmount_tr').hide();
            formalStudent_lostStudent_dialog.dialog("open");
        },
        /*退学保存zj*/
        saveLost:function () {
            var url = "/leavingStudent/save";
            formalStudent_lostStudent_form.form('submit', {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('提示', "操作成功", 'info', function () {
                            cmdObj.cancelLost();
                            cmdObj.reloadStudent();
                        });
                    } else {
                        $.messager.alert('提示', data.msg, 'error')
                    }
                }
            });
        },
        /*退学弹窗关闭*/
        cancelLost:function () {
            formalStudent_lostStudent_dialog.dialog("close");
        },
        /*升班按钮点击事件*/
        upgrade:function () {
            formalStudent_upgrade_form.form("clear");
            formalStudent_upgrade_dialog.dialog("open");
        },
        /*升班弹框关闭*/
        cancelUpgrade:function () {
            formalStudent_upgrade_dialog.dialog("close");
        },
        /*升班保存*/
        saveUpgrade:function () {
            formalStudent_upgrade_form.form('submit', {
                url: '/formalStudent/upgrade',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('提示', "操作成功", 'info', function () {
                            cmdObj.cancelUpgrade();
                            cmdObj.reloadStudent();
                        });
                    } else {
                        $.messager.alert('提示', data.msg, 'error')
                    }
                }
            });
        }

    };

    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });
    $("[name='moneyback']").change(function () {
        if ($(this).val() == 0) {
            // 隐藏收款按钮
            $('#backMoneyAmount_tr').hide();
        } else {
            $('#backMoneyAmount_tr').show();
        }
    });
})
